﻿using System;
using System.Data;
using System.Drawing;
using Maticsoft.Common;
using QunYing.Common;
using QunYing.Model;
using Spore.Validator;
using System.Transactions;
using Client.OrderModule;
using System.ServiceModel;

namespace Client
{
    public partial class FrmOrderDetail : BasePopForm
    {
        private DataTable dtReport = null;
        private OrderInfo modelReport = null;
        public FrmOrderDetail()
        {
            InitializeComponent();
            cbxAddSub.Text = "+";
            this.NecessaryRemark(this.cbxState);
            this.FormValidator.SetControlValitors(cbxState, ValidatorDefaults.NotNull);
            //this.FormValidator.SetControlValitors(cbxState, ValidatorDefaults.Integer);
        }

        public int id = 0;
        private string userId = "0";
        private void FrmOrderDetail_Load(object sender, EventArgs e)
        {
            BindData();
        }

        private void cbxState_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cbxState.Text == Enums.End)
            {
                lblNum.Visible = true;
                cbxAddSub.Visible = true;
                txtNum.Visible = true;
                lblError.Visible = true;
            }
            else
            {
                lblNum.Visible = false;
                cbxAddSub.Visible = false;
                txtNum.Visible = false;
                lblError.Visible = false;
            }
        }

        public void BindData()
        {
            QunYing.Model.OrderInfo model = new QunYing.BLL.OrderInfo().GetModel(id);

            DataTable dt = new QunYing.BLL.OrderDetail().GetList("订单ID=" + model.订单ID).Tables[0];
            gridControl1.DataSource = dt;
            dtReport = dt;
            modelReport = model;
            lblTotalPrice.Text = model.总价 == null ? "￥0.00" : "￥" + Convert.ToDecimal(model.总价).ToString("f2");
            lblUseScore.Text = model.总积分 == null ? "0" : Convert.ToDecimal(model.总积分).ToString("f0");
            lblGetScore.Text = model.赠送总积分 == null ? "0" : Convert.ToDecimal(model.赠送总积分).ToString("f0");
            lblOrderNo.Text = model.订单编号;
            lblUser.Text = model.收件人;
            lblAddress.Text = model.收件地址;
            lblTel.Text = model.联系电话;
            lblMsg.Text = model.用户留言;
            userId = model.用户ID;
            DataTable dt2 =
                new QunYing.BLL.OrderChange().GetList("订单ID=" + model.订单ID + " order by 操作时间 DESC").Tables[0];
            gridControl2.DataSource = dt2;

            cbxState.Properties.Items.Clear();
            if (dt2.Rows.Count > 0)
            {
                switch (dt2.Rows[0]["订单状态"].ToString())
                {
                    case Enums.Start:
                        cbxState.Properties.Items.Add(Enums.Ready);
                        cbxState.Properties.Items.Add(Enums.Canel);
                        cbxState.Properties.Items.Add(Enums.Error);
                        break;
                    case Enums.Ready:
                        cbxState.Properties.Items.Add(Enums.Send);
                        cbxState.Properties.Items.Add(Enums.Canel);
                        cbxState.Properties.Items.Add(Enums.Error);
                        break;
                    case Enums.Send:
                        cbxState.Properties.Items.Add(Enums.End);
                        cbxState.Properties.Items.Add(Enums.Canel);
                        cbxState.Properties.Items.Add(Enums.Error);
                        break;
                    case Enums.End:
                        cbxState.Properties.Items.Add(Enums.End);
                        btnSave.Enabled = false;
                        break;
                    case Enums.Error:
                        cbxState.Properties.Items.Add(Enums.Error);
                        btnSave.Enabled = false;
                        break;
                    case Enums.Canel:
                        cbxState.Properties.Items.Add(Enums.Canel);
                        btnSave.Enabled = false;
                        break;
                    default:
                        cbxState.Properties.Items.Add("订单异常");
                        btnSave.Enabled = false;
                        break;
                }
                cbxState.SelectedIndex = 0;
            }
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (!this.FormValidator.ValidAll()) return;
            if (txtNum.Text.Trim() != "")
            {
                if (!PageValidate.IsNumber(txtNum.Text.Trim()))
                {
                    GlobalInfo.MsgBoxShowError("退还积分格式错误！");
                    return;
                }
            }
            if (cbxState.Text == Enums.End && txtNum.Text.Trim() != "")
            {
                if (cbxAddSub.Text == "+")
                {
                    if (Convert.ToInt32(txtNum.Text) > Convert.ToInt32(lblUseScore.Text))
                    {
                        GlobalInfo.MsgBoxShowError("返还积分超过订单消耗总积分！");
                        return ;
                    }
                }
                else
                {
                    if (Convert.ToInt32(txtNum.Text) > Convert.ToInt32(lblGetScore.Text))
                    {
                        GlobalInfo.MsgBoxShowError("扣减积分超过订单赠送总积分！");
                        return ;
                    }
                }
            }
            if (HandleOrder())
            {
                FrmOrderManager.frm.QueryData(true);
                this.Close();
            }
        }

        private bool HandleOrder()
        {
            QunYing.Model.OrderChange model = new QunYing.Model.OrderChange();
            model.订单ID = id;
            model.操作人 = GlobalInfo.UserLoginInfo.用户ID;
            model.操作时间 = DateTime.Now;
            model.订单状态 = cbxState.Text;
            model.备注 = txtDesc.Text.Trim();
            int i = new QunYing.BLL.OrderChange().Add(model);
            if (i > 0)
            {
                //BindData();
                QunYing.Model.Score score = new QunYing.BLL.Score().GetModel(userId);
                if (score == null)
                {
                    score = new Score();
                    score.用户ID = userId;
                    score.积分 = 0;
                    new QunYing.BLL.Score().Add(score);
                }
                if (cbxState.Text == Enums.Canel || cbxState.Text == Enums.Error)
                {
                    score.积分 = score.积分 + Convert.ToInt32(lblUseScore.Text) - Convert.ToInt32(lblGetScore.Text);
                }
                if (cbxState.Text == Enums.End && txtNum.Text.Trim() != "")
                {
                    
                    score.积分 = cbxAddSub.Text == "+"
                                   ? score.积分 + Convert.ToInt32(txtNum.Text)
                                   : score.积分 - Convert.ToInt32(txtNum.Text);

                }
                QunYing.BLL.Score scoreBLL = new QunYing.BLL.Score();
                scoreBLL.Update(score);
            }
            else
            {
                GlobalInfo.MsgBoxShowError("处理失败！");
                return false;
            }
            return true;
        }

        private void btnMap_Click(object sender, EventArgs e)
        {
            FrmGisMap frm = SingleObjManager.GetSingleObj<FrmGisMap>();
            frm.strUserID = userId;
            frm.ShowDialog();
        }

        private void btnPrint_Click(object sender, EventArgs e)
        {
            QunYing.Model.VipInfo model = new QunYing.BLL.VipInfo().GetModelByID(userId);
            var opera = ClientProxyFactory.GetProxy<IImageOperator>(QunYing.Common.GlobalInfo.ServiceImageOperationVip);
            Bitmap[] bp = opera.GetImages(GlobalInfo.UserLoginInfo.用户ID, new string[] { model.会员卡号 + ".jpg" });
            Bitmap b = null;
            if (bp.Length > 0)
            {
                b = bp[0];
            }
            OrderReport report = new OrderReport(dtReport, modelReport, b);
            report.ShowPreviewDialog();
            (opera as ICommunicationObject).Close();
        }
    }
}